package com.baijiayun.live.ui.chat.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.widget.TextView;

import com.baijiayun.glide.Glide;
import com.baijiayun.glide.load.resource.gif.GifDrawable;
import com.baijiayun.glide.request.target.SimpleTarget;
import com.baijiayun.glide.request.transition.Transition;

public class URLImageParser implements Drawable.Callback{
    private final TextView mTextView;
    private final float mImageSize;

    /**
     *
     * @param textView 图文混排TextView
     * @param imageSize 图片显示高度
     */
    public URLImageParser(TextView textView, float imageSize) {
        mTextView = textView;
        mImageSize = imageSize;
    }

    public Drawable getDrawable(String url) {
        if (url.endsWith(".gif")) {
            GlideUrlDrawable glideDrawable = new GlideUrlDrawable();
            Glide.with(mTextView.getContext()).asGif().load(url).into(new SimpleTarget<GifDrawable>() {
                @Override
                public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) {
                    Rect rect = new Rect(0, 0, (int) (mImageSize * 1.8f),(int) (mImageSize * 1.8f));
                    resource.setBounds(rect);
                    glideDrawable.setBounds(rect);
                    glideDrawable.setDrawable(resource);
                    glideDrawable.setCallback(URLImageParser.this);
                    resource.setLoopCount(GifDrawable.LOOP_FOREVER);
                    resource.start();
                    mTextView.invalidate();
                    mTextView.setText(mTextView.getText());
                }
            });
            return glideDrawable;
        } else {
            URLDrawable urlDrawable = new URLDrawable();
            Glide.with(mTextView.getContext()).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    urlDrawable.bitmap = changeBitmapSize(resource, mImageSize);
                    urlDrawable.setBounds(0, 0, urlDrawable.bitmap.getWidth(), urlDrawable.bitmap.getHeight());
                    mTextView.invalidate();
                    mTextView.setText(mTextView.getText());
                }
            });
            return urlDrawable;
        }
    }
    private Bitmap changeBitmapSize(Bitmap bitmap, float mImageSize) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float newWidth = mImageSize * 1.6f;
        float newHeight = height * newWidth / width;
        float scaleWidth = newWidth / width;
        float scaleHeight = newHeight / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return bitmap;
    }

    @Override
    public void invalidateDrawable(@NonNull Drawable who) {
        if (mTextView != null) {
            mTextView.invalidate();
        }
    }

    @Override
    public void scheduleDrawable(@NonNull Drawable who, @NonNull Runnable what, long when) {

    }

    @Override
    public void unscheduleDrawable(@NonNull Drawable who, @NonNull Runnable what) {

    }

    public static class URLDrawable extends BitmapDrawable{
        public Bitmap bitmap;

        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
            if (bitmap != null) {
                canvas.drawBitmap(bitmap, 0, 0, getPaint());
            }
        }
    }
    public static class GlideUrlDrawable extends Drawable implements Drawable.Callback{
        private GifDrawable mDrawable;

        @Override
        public void draw(Canvas canvas) {
            if (mDrawable != null) {
                mDrawable.draw(canvas);
            }
        }

        @Override
        public void setAlpha(int alpha) {
            if (mDrawable != null) {
                mDrawable.setAlpha(alpha);
            }
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
            if (mDrawable != null) {
                mDrawable.setColorFilter(cf);
            }
        }

        @Override
        public int getOpacity() {
            if (mDrawable != null) {
                return mDrawable.getOpacity();
            }
            return 0;
        }

        public void setDrawable(GifDrawable drawable) {
            if (this.mDrawable != null) {
                this.mDrawable.setCallback(null);
            }
            drawable.setCallback(this);
            this.mDrawable = drawable;
        }

        @Override
        public void invalidateDrawable(Drawable who) {
            if (getCallback() != null) {
                getCallback().invalidateDrawable(who);
            }
        }

        @Override
        public void scheduleDrawable(Drawable who, Runnable what, long when) {
            if (getCallback() != null) {
                getCallback().scheduleDrawable(who, what, when);
            }
        }

        @Override
        public void unscheduleDrawable(Drawable who, Runnable what) {
            if (getCallback() != null) {
                getCallback().unscheduleDrawable(who, what);
            }
        }
    }
}
